package com.ruoyi.waterManagement.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.ruoyi.common.core.domain.business.waterManagement.WrStatvolY;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.business.BusinessUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.waterManagement.domain.vo.RzsWrStatvolHwrVO;
import com.ruoyi.waterManagement.domain.vo.WaterReportVO;
import com.ruoyi.waterManagement.mapper.WrStatvolHMapper;
import com.ruoyi.waterManagement.mapper.WrStatvolYMapper;
import com.ruoyi.waterManagement.service.IWrStatvolYService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 * 测站年水量数据Service业务层处理
 * 
 * @author hwd
 * @date 2023-02-20
 */
@Service
public class WrStatvolYServiceImpl implements IWrStatvolYService
{
    @Autowired
    private WrStatvolYMapper wrStatvolYMapper;
    @Autowired
    private WrStatvolHMapper wrStatvolHMapper;
    /**
     * 查询测站年水量数据
     * 
     * @param statCd 测站年水量数据主键
     * @return 测站年水量数据
     */
    @Override
    public WrStatvolY selectWrStatvolYByStatCd(String statCd)
    {
        return wrStatvolYMapper.selectWrStatvolYByStatCd(statCd);
    }

    /**
     * 查询测站年水量数据列表
     * 
     * @param wrStatvolY 测站年水量数据
     * @return 测站年水量数据
     */
    @Override
    public List<WrStatvolY> selectWrStatvolYList(WrStatvolY wrStatvolY)
    {

        return wrStatvolYMapper.selectWrStatvolYList(wrStatvolY);
    }

    /**
     * 新增测站年水量数据
     * 
     * @param wrStatvolY 测站年水量数据
     * @return 结果
     */
    @Override
    public int insertWrStatvolY(WrStatvolY wrStatvolY)
    {
        return wrStatvolYMapper.insertWrStatvolY(wrStatvolY);
    }

    /**
     * 修改测站年水量数据
     * 
     * @param wrStatvolY 测站年水量数据
     * @return 结果
     */
    @Override
    public int updateWrStatvolY(WrStatvolY wrStatvolY)
    {
        return wrStatvolYMapper.updateWrStatvolY(wrStatvolY);
    }

    /**
     * 批量删除测站年水量数据
     * 
     * @param statCds 需要删除的测站年水量数据主键
     * @return 结果
     */
    @Override
    public int deleteWrStatvolYByStatCds(String[] statCds)
    {
        return wrStatvolYMapper.deleteWrStatvolYByStatCds(statCds);
    }

    /**
     * 删除测站年水量数据信息
     * 
     * @param statCd 测站年水量数据主键
     * @return 结果
     */
    @Override
    public int deleteWrStatvolYByStatCd(String statCd)
    {
        return wrStatvolYMapper.deleteWrStatvolYByStatCd(statCd);
    }


    @Override
    public List<WaterReportVO> selectYearWaterReport(WaterReportVO waterReportVO) {
        return listYearWaterReport(waterReportVO);
    }

    @Override
    public void exportYearWaterReport(WaterReportVO waterReportVO, HttpServletResponse response) throws IOException {
        int yy=1970;
        if (waterReportVO.getMm()==null){
            Calendar calendar= Calendar.getInstance();
            calendar.setTime(new Date());
            yy=calendar.get(Calendar.YEAR);
        }else {
            yy=Integer.parseInt(waterReportVO.getYy());
        }
        String excelTitle = waterReportVO.getOrgNm() +yy+"年" + "水量年报";
        InputStream inputStream = null;
        List<WaterReportVO> list = null;
        WaterReportVO sumVO = null;
        inputStream = this.getClass().getClassLoader().getResourceAsStream("static/file/水量年报.xlsx");
        list = listYearWaterReport(waterReportVO);
        List<WaterReportVO> result = new ArrayList<>();
        result.addAll(list);
        // 添加表头和日期
        Map<String, String> map = new HashMap<>();
        map.put("excelTitle", excelTitle);
        ServletOutputStream out = response.getOutputStream();
        ExcelWriter excelWriter = EasyExcel.write(out, WaterReportVO.class).withTemplate(inputStream).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        excelWriter.fill(result, writeSheet);
        excelWriter.fill(map, writeSheet);
        excelWriter.finish();

    }

    public List<WaterReportVO> listYearWaterReport(WaterReportVO yearWaterReportVO){
        /**入参水管机构 日期
         */
        //判断关键参数日期是否存在
        String wrdate=yearWaterReportVO.getYy();
        if(StringUtils.isNotEmpty(wrdate)){
            //晚于当前日期或者没有日期，则改为当前日期
        }else{
            wrdate= DateUtils.parseDateToStr(DateUtils.YYYY,new Date());
        }
        yearWaterReportVO.setYy(wrdate);
        if (StringUtils.isEmpty(yearWaterReportVO.getOrgCd())) {
            yearWaterReportVO.setOrgCd(null);
        }
        //找到下属的所有水管机构的统计
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(yearWaterReportVO.getOrgCd());
        int grade = (int) addvAndOrgCodeInfo.get("grade");
        String resCode = (String) addvAndOrgCodeInfo.get("resCode");
        yearWaterReportVO.setOrgCd(resCode+"%");
        List<WaterReportVO> yearWaterReportVOList = wrStatvolYMapper.selectYearWaterReport(yearWaterReportVO);
        if (grade == 1){
            //查找下属水管机构
            resCode =  resCode + "%0000";
        }else if (grade == 2) {
            //查找下属水管机构
            resCode =resCode + "%00";
        }else if(grade ==3) {
            resCode =resCode+"%";
        }else {
            resCode = resCode;
        }
        //查找下属机构
        RzsWrStatvolHwrVO rzsWrStatvolHwrVO=new RzsWrStatvolHwrVO();
        rzsWrStatvolHwrVO.setOrgCd(resCode);
        List<RzsWrStatvolHwrVO> orglist = wrStatvolHMapper.listOrgCd(rzsWrStatvolHwrVO);
        List<WaterReportVO> orgnewList =new ArrayList<>();
        //遍历查询下属水管机构统计
        for (RzsWrStatvolHwrVO r:orglist
        ) {
            HashMap<String, Object> stringObjectHashMap = BusinessUtils.getAddvAndOrgCodeInfo(r.getOrgCd());
            String ro = (String) stringObjectHashMap.get("resCode");
            List<WaterReportVO> yearWaterReportList = yearWaterReportVOList.stream().filter(s -> s.getOrgCd().startsWith(ro)).collect(Collectors.toList());
            WaterReportVO wrVoSum =new WaterReportVO();
            BigDecimal yrWaterVol =new BigDecimal("0");
            wrVoSum.setOrgNm(r.getOrgNm());
            if (yearWaterReportList.size()>0) {
                for (WaterReportVO w : yearWaterReportList
                ) {
                    yrWaterVol = w.getYrVol() == null ? yrWaterVol.add(new BigDecimal("0")) : yrWaterVol.add(w.getYrVol()).setScale(4, BigDecimal.ROUND_HALF_UP);
                }
            }
            wrVoSum.setOrgCd("合计");
            wrVoSum.setYrVol(yrWaterVol);
            orgnewList.add(0,wrVoSum);
        }
        orgnewList.removeAll(Collections.singleton(null));
        //将数据结果放入map中区分时
        Collections.reverse(orgnewList);
        //将数据结果放入map中区分时
        return orgnewList;

    }

}
